package com.yeyks.controller.other;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.MattressInfoParam;
import com.yeyks.common.param.MattressPackageConfigInsertParam;
import com.yeyks.common.result.HotelNameLikeResult;
import com.yeyks.common.result.MattressInfoResult;
import com.yeyks.commonReference.service.mattress.MattressInfoService;
import com.yeyks.mattress.dal.domain.MattressPackageConfig;
import com.yeyks.mattress.service.MattressPackageConfigService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 共享床垫接口
 *
 * @author shuai
 * @date 2019-07-09
 */
@Slf4j
@Api(tags = "共享床垫接口")
@RestController
@RequestMapping("/mattress")
public class MattressInfoController {

    @Autowired
    private MattressInfoService mattressInfoService;
    @Autowired
    private MattressPackageConfigService packageConfigService;

    /**
     * 获取酒店床垫列表
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "获取酒店床垫列表", notes = "获取酒店床垫列表", response = MattressInfoResult.class)
    @PostMapping("/list")
    public AjaxResponseBody<PageResult<MattressInfoResult>> list(MattressInfoParam param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(mattressInfoService.myList(param));
    }

    /**
     * 通过酒店名称模糊查询
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "v4.0.0 --> 通过酒店名称模糊查询", notes = "通过酒店名称模糊查询", response = HotelNameLikeResult.class)
    @ApiImplicitParam(paramType = "query", name = "hotelName", value = "酒店名称", required = true, dataType = "String")
    @PostMapping("/hotelNameLike")
    public AjaxResponseBody<List<HotelNameLikeResult>> hotelNameLike(@RequestParam(name = "hotelName") String hotelName) {
        if (Arguments.strIsNull(hotelName)) {
            throw new ServiceException("酒店名不能为空");
        }
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(mattressInfoService.hotelNameLike(hotelName));
    }

    /**
     * 绑定酒店与设备
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "绑定酒店与设备", notes = "绑定成功返回true，失败直接抛出异常")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "设备序号", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "hotelId", value = "酒店id", required = true, dataType = "int")
    })
    @PostMapping("/bind")
    public AjaxResponseBody<Boolean> bind(@RequestParam(name = "id") Integer id,
                                          @RequestParam(name = "hotelId") Integer hotelId) {
        if (null == id) {
            throw new ServiceException("设备序号必传");
        }
        if (null == hotelId) {
            throw new ServiceException("酒店id必传");
        }
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(mattressInfoService.bind(id, hotelId, loginUser));
    }

    /**
     * 新增床垫
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "新增床垫", notes = "新增床垫成功返回true，失败直接抛出异常")
    @ApiImplicitParam(paramType = "query", name = "imeiId", value = "床垫编号", required = true, dataType = "String")
    @PostMapping("/insert")
    public AjaxResponseBody<Boolean> insert(@RequestParam(name = "imeiId") String imeiId) {
        if (Arguments.strIsNull(imeiId)) {
            throw new ServiceException("床垫编号必传");
        }
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(mattressInfoService.insert(imeiId, loginUser));
    }

    /**
     * 套餐列表
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "套餐列表", notes = "套餐列表")
    @PostMapping("/config/list")
    public AjaxResponseBody<List<MattressPackageConfig>> configList() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(packageConfigService.list(new QueryWrapper<MattressPackageConfig>().notIn(MattressPackageConfig.STATUS, -1)));
    }

    /**
     * 新增套餐
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "新增套餐", notes = "新增套餐成功返回true，失败直接抛出异常")
    @PostMapping("/config/insert")
    public AjaxResponseBody<Boolean> configInsert(@Validated MattressPackageConfigInsertParam param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(packageConfigService.configInsert(param));
    }

    /**
     * 修改套餐
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "修改套餐", notes = "修改套餐成功返回true，失败直接抛出异常")
    @PostMapping("/config/update")
    public AjaxResponseBody<Boolean> configUpdate(MattressPackageConfig param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(packageConfigService.configUpdate(param));
    }

    @NeedToken
    @ApiOperation(value = "删除床垫套餐配置", notes = "删除床垫套餐配置，失败直接抛出异常")
    @PostMapping("/config/delete")
    @SystemLog(value = SysLogMsg.MATTRESS_DEL)
    public AjaxResponseBody<Boolean> deleteConfigPackage(Integer id) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(packageConfigService.deleteConfigPackage(id));
    }


}
